const Op = require('sequelize').Op;
const sequelize = require('sequelize');
class ArticleController {
    async getArticle(ctx) { //查询全部文章
        const { limit = 40, tab = undefined, page = 1 } = ctx.query;
        let data = {};
        if (tab == undefined || tab == '' || tab == 'undefined') {
            data.article = await ctx.app.Service.Article.findAll({
                where: { category_id: { [Op.ne]: 4 } },
                order: [['is_top', 'DESC'], ['created_at', 'DESC']],
                limit: Number(limit), // 当页条数
                offset: Number(limit * (page - 1)) || 0, // 开始下标 
                include: [
                    {
                        model: ctx.app.Service.Replie,
                        separate: true,
                        order: [['created_at', 'DESC']],
                        include: [
                            {
                                model: ctx.app.Service.User,
                                attributes: {
                                    exclude: ['password'] //排除此字段返回其余字段
                                },
                            }
                        ]
                    },
                    {
                        model: ctx.app.Service.User,
                        attributes: {
                            exclude: ['password'] //排除此字段返回其余字段
                        },
                    },
                    {
                        model: ctx.app.Service.Categorie,
                    }
                ]
            });
            data.total = await ctx.app.Service.Article.count({ where: { category_id: { [Op.ne]: 4, } } })
            data.limit = limit; data.page = page; data.tab = tab || '';
        } else if (tab == 0) {
            data = await ctx.app.Controller.article.getArticleBoutique(ctx);
        } else {
            data = await ctx.app.Controller.article.getArticleCategory(ctx);
        }
        ctx.body = {
            code: 200,
            message: '成功',
            data
        }
    }
    async getArticleBoutique(ctx) { //查询全部精品文章
        const { limit = 40, tab = undefined, page = 1 } = ctx.query;
        let data = {};
        data.article = await ctx.app.Service.Article.findAll({
            where: {
                view_count: { [Op.gte]: 1000, },
                category_id: { [Op.ne]: 4 },
            },
            order: [['is_top', 'DESC'], ['created_at', 'DESC']],
            limit: Number(limit), // 当页条数
            offset: Number(limit * (page - 1)) || 0, // 开始下标 
            include: [
                {
                    model: ctx.app.Service.Replie,
                    separate: true,
                    order: [['created_at', 'DESC']],
                    include: [
                        {
                            model: ctx.app.Service.User,
                            attributes: {
                                exclude: ['password'] //排除此字段返回其余字段
                            },
                        }
                    ]
                },
                {
                    model: ctx.app.Service.User,
                    attributes: {
                        exclude: ['password'] //排除此字段返回其余字段
                    },
                },
                {
                    model: ctx.app.Service.Categorie,
                }
            ]
        });
        data.total = await ctx.app.Service.Article.count({ where: { view_count: { [Op.gte]: 1000 }, category_id: { [Op.ne]: 4 } } })
        data.limit = limit; data.page = page; data.tab = tab;
        return data;
    }
    async getArticleCategory(ctx) { //分类查询文章
        const { limit = 40, tab = undefined, page = 1 } = ctx.query;
        let data = {};
        data.article = await ctx.app.Service.Article.findAll({
            where: { category_id: tab },
            order: [['is_top', 'DESC'], ['created_at', 'DESC']],
            limit: Number(limit), // 当页条数
            offset: Number(limit * (page - 1)) || 0, // 开始下标 
            include: [
                {
                    model: ctx.app.Service.Replie,
                    separate: true,
                    order: [['created_at', 'DESC']],
                    include: [
                        {
                            model: ctx.app.Service.User,
                            attributes: {
                                exclude: ['password'] //排除此字段返回其余字段
                            },
                        }
                    ]
                },
                {
                    model: ctx.app.Service.User,
                    attributes: {
                        exclude: ['password'] //排除此字段返回其余字段
                    },
                },
                {
                    model: ctx.app.Service.Categorie,
                }
            ]
        });
        data.total = await ctx.app.Service.Article.count({ where: { category_id: tab } })
        data.limit = limit; data.page = page; data.tab = tab;
        return data;
    }
    async getUnansweredTopic(ctx) { //获取未回复话题
        const { limit = 5, page = 1 } = ctx.query;
        const list = await ctx.app.Service.Replie.findAll({
            group: 'article_id',
            attributes: [['article_id', 'id']],
        })
        const data = await ctx.app.Service.Article.findAll({
            where: {
                [Op.not]: {
                    [Op.or]: JSON.parse(JSON.stringify(list))
                },
                category_id: { [Op.ne]: 4 }
            },
            order: [['created_at', 'DESC']],
            limit: Number(limit), // 当页条数
            offset: Number(limit * (page - 1)) || 0, // 开始下标 
        })
        ctx.body = data;
    }
    async getUserOtherTopic(ctx) {   //获取作者其他话题
        const { limit = 5, page = 1, id = '' } = ctx.query;
        const data = await ctx.app.Service.Article.findAll({
            where: { user_id: id },
            limit: Number(limit), // 当页条数
            offset: Number(limit * (page - 1)) || 0, // 开始下标 
        })
        ctx.body = data;
    }
    async getArticleContent(ctx) {   //获取文章详情
        const { id = '' } = ctx.query;
        let data = await ctx.app.Service.Article.findOne({
            where: {
                origin_id: id,
            },
            include: [
                {
                    model: ctx.app.Service.User,
                    attributes: {
                        exclude: ['password'] //排除此字段返回其余字段
                    },
                },
                {
                    model: ctx.app.Service.Replie,
                    include: [
                        {
                            model: ctx.app.Service.User,
                            attributes: {
                                exclude: ['password'] //排除此字段返回其余字段
                            },
                        }
                    ]
                },
                { model: ctx.app.Service.Categorie },
                { model: ctx.app.Service.Article_content },
            ]
        })
        if (data != null) {
            await ctx.app.Service.Article.update(   //修改阅读量
                { view_count: ++data.dataValues.view_count },
                { where: { origin_id: id, } }
            )
            data.dataValues.view_count = data.dataValues.view_count++;
            const marked = require('marked');
            data.dataValues.article_contents[0].dataValues.content = marked.parse(data.dataValues.article_contents[0].dataValues.content);
            for (let i = 0; i < data.dataValues.replies.length; i++) {
                data.dataValues.replies[i].dataValues.content = marked.parse(data.dataValues.replies[i].dataValues.content);
            }
        }
        ctx.body = data;
    }
    async getUserArticle(ctx) {    //通过username获取最近创建的话题
        const { username = "", limit = 5, page = 1, } = ctx.query;
        let data = {};
        data.user = await ctx.app.Service.User.findOne({
            where: { username },
            attributes: {
                exclude: ['password'] //排除此字段返回其余字段
            },
            include: [
                {
                    model: ctx.app.Service.Article,
                    limit: Number(limit), // 当页条数
                    offset: Number(limit * (page - 1)) || 0, // 开始下标 
                    separate: true,
                    order: [['created_at', 'DESC']],
                    include: [
                        {
                            model: ctx.app.Service.Replie,
                            separate: true,
                            order: [['created_at', 'DESC']],
                            include: [
                                {
                                    model: ctx.app.Service.User,
                                    attributes: {
                                        exclude: ['password'] //排除此字段返回其余字段
                                    },
                                },
                            ]
                        },
                        {
                            model: ctx.app.Service.User,
                            attributes: {
                                exclude: ['password'] //排除此字段返回其余字段
                            },
                        },
                    ]
                },
            ]
        })
        data.total = await ctx.app.Service.User.count({
            where: { username },
            include: [
                {
                    model: ctx.app.Service.Article,
                }
            ]
        })
        data.limit = limit; data.page = page;
        ctx.body = data;
    }
    async getUserReplie(ctx) {    //通过username获取最近参与的话题
        const { username = "", limit = 5, page = 1, } = ctx.query;
        let data = {};
        data.user = await ctx.app.Service.User.findOne({
            where: { username },
            attributes: {
                exclude: ['password'] //排除此字段返回其余字段
            },
            include: [
                {
                    model: ctx.app.Service.Replie,
                    separate: true,
                    order: [['created_at', 'DESC']],
                    limit: Number(limit), // 当页条数
                    offset: Number(limit * (page - 1)) || 0, // 开始下标 
                    include: [
                        {
                            model: ctx.app.Service.Article,
                            // separate: true,
                            order: [['created_at', 'DESC']],
                            include: [
                                {
                                    model: ctx.app.Service.User,
                                    attributes: {
                                        exclude: ['password'] //排除此字段返回其余字段
                                    },
                                },
                                {
                                    model: ctx.app.Service.Replie,
                                    separate: true,
                                    order: [['created_at', 'DESC']],
                                    include: [
                                        {
                                            model: ctx.app.Service.User,
                                            attributes: {
                                                exclude: ['password'] //排除此字段返回其余字段
                                            },
                                        },
                                    ]
                                },
                            ]
                        }
                    ]
                },
            ]
        })
        data.total = await ctx.app.Service.User.count({
            where: { username },
            include: [
                {
                    model: ctx.app.Service.Replie,
                }
            ]
        })
        data.limit = limit; data.page = page;
        ctx.body = data;
    }
}
module.exports = ArticleController;
